package com.yyoo.doc.mypdf.builder;

import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.yyoo.doc.html.style.StyleAttr;

/**
 * 解析文本类型的标签
 *
 * @author zzy
 */
public class PdfParagraphBuilder extends PdfElementBuilder {

    @Override
    protected void initDefaultStyle() {
        String tagName = getHtmlEl().getTagName();
        if("h1".equalsIgnoreCase(tagName)){
            this.addStyle(StyleAttr.FONT_SIZE, "2em");
            this.addStyle(StyleAttr.FONT_STYLE, "bold");
        }else if("h2".equalsIgnoreCase(tagName)){
            this.addStyle(StyleAttr.FONT_SIZE, "1.5em");
            this.addStyle(StyleAttr.FONT_STYLE, "bold");
        }else if("h3".equalsIgnoreCase(tagName)){
            this.addStyle(StyleAttr.FONT_SIZE, "1.17em");
            this.addStyle(StyleAttr.FONT_STYLE, "bold");
        }else if("h4".equalsIgnoreCase(tagName)){
            this.addStyle(StyleAttr.FONT_SIZE, "1em");
            this.addStyle(StyleAttr.FONT_STYLE, "bold");
        }else if("h5".equalsIgnoreCase(tagName)){
            this.addStyle(StyleAttr.FONT_SIZE, "0.83em");
            this.addStyle(StyleAttr.FONT_STYLE, "bold");
        }else if("h6".equalsIgnoreCase(tagName)){
            this.addStyle(StyleAttr.FONT_SIZE, "0.67em");
            this.addStyle(StyleAttr.FONT_STYLE, "bold");
        }
    }

    @Override
    public Element build() {
        // 只获取标签本身的文本，子标签的文本不获取
        String text = this.getHtmlEl().getOwnTextString();

        Paragraph paragraph = new Paragraph(text, this.getFont());
        paragraph.setAlignment(this.getTextAlign());

        return paragraph;
    }

}
